package matlap.client.view.admin;

import java.util.Iterator;
import java.util.List;

import matlap.client.services.distributor.DistributorService;
import matlap.client.services.distributor.DistributorServiceAsync;
import matlap.client.services.order.OrderService;
import matlap.client.services.order.OrderServiceAsync;
import matlap.shared.Distributor;
import matlap.shared.Order;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.client.ui.IntegerBox;

public class OrdersComposite extends Composite {

	OrderServiceAsync c1 = GWT.create(OrderService.class);
	DistributorServiceAsync c2 = GWT.create(DistributorService.class);
	private VerticalPanel vp;
	private Grid g;
	private Label lbName;
	private TextBox tbName;
	private Button btnSearch;
	protected String name;
	VerticalPanel simplePanel = new VerticalPanel();
	Panel newPanel = new HorizontalPanel();
	Panel gridPanel = new VerticalPanel();
	private ListBox comboBox;
	private Label lblNewLabel_1;
	private Button btnNewButton;
	private DateBox dateBox;
	private IntegerBox integerBox;

	public OrdersComposite() {
		init();
		initGrid();

	}

	/*
	 * public void updateButtonColor(int c1,int c2, int c3){ if (c1==0){
	 * newButton.setStyleName("Button1"); } else {
	 * newButton.setStyleName("Button2"); } if (c2==0){
	 * updateButton.setStyleName("Button1"); } else {
	 * updateButton.setStyleName("Button2"); }
	 * 
	 * if (c3==0){ deleteButton.setStyleName("Button1"); } else {
	 * deleteButton.setStyleName("Button2"); }
	 * 
	 * }
	 */
	public void init() {
		vp = new VerticalPanel();
		initWidget(vp);
		vp.setSize("670px", "500px");

		DecoratorPanel decoratorPanel = new DecoratorPanel();
		vp.add(decoratorPanel);
		Label lblNewLabel_2 = new Label("\u00DAj rendel\u00E9s:");
		lblNewLabel_2.setStyleName("BigBold");
		decoratorPanel.setWidget(lblNewLabel_2);

		initNewPanel();
		simplePanel.setBorderWidth(1);
		simplePanel.add(newPanel);
		vp.add(simplePanel);

		DecoratorPanel decoratorPanel2 = new DecoratorPanel();
		vp.add(decoratorPanel2);
		Label lblNewLabel_3 = new Label("A jelenlegi rendel\u00E9sek:");
		lblNewLabel_3.setStyleName("BigBold");
		decoratorPanel2.setWidget(lblNewLabel_3);
		
		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setBorderWidth(1);
		vp.add(verticalPanel);

		FlexTable filterFlex = new FlexTable();
		verticalPanel.add(filterFlex);
		filterFlex.setSize("687px", "39px");

		lbName = new Label("N\u00E9v");
		filterFlex.setWidget(0, 0, lbName);

		tbName = new TextBox();
		filterFlex.setWidget(1, 0, tbName);

		btnSearch = new Button("New button");
		btnSearch.setText("Keres");
		filterFlex.setWidget(1, 1, btnSearch);


		btnSearch.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
			    name = tbName.getText();

				c1.getOrdersByName(name, new AsyncCallback<List<Order>>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(List<Order> result) {
						int i = 1;
						Iterator<Order> it = result.iterator();
						g.resize(result.size() + 1, 3);
						while (it.hasNext()) {
							Order or = (Order) it.next();

							g.setText(i, 0, or.getWho());
							g.setText(i, 1, or.getUserName());
							g.setText(i, 1, or.getDate());
							g.setText(i, 2, String.valueOf(or.getNr()));
							i++;

						}

					}
				});
			}
		});

	}

	public void initGrid() {
		g = new Grid(1, 4);
		
		Label lb1 = new Label("N\u00E9v");
		lb1.setStyleName("Header");
		g.setWidget(0, 0, lb1);
		
		Label lb2 = new Label("Felhaszn\u00E1lon\u00E9v");
		lb2.setStyleName("Header");
		g.setWidget(0, 1, lb2);
		
		Label lb3 = new Label("D\u00E1tum");
		lb3.setStyleName("Header");
		g.setWidget(0, 2, lb3);
		
		Label lb4 = new Label("Darab");
		lb4.setStyleName("Header");
		g.setWidget(0, 3, lb4);
		
		g.setBorderWidth(2);
		gridPanel.add(g);
		vp.add(gridPanel);
		gridPanel.setWidth("687px");
		g.setSize("686px", "34px");
		c1.getOrdersNumber(new AsyncCallback<Integer>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Gond van nr");

			}

			@Override
			public void onSuccess(Integer result) {
				// TODO Auto-generated method stub
				g.resize(result + 1, 4);
			}
		});

		c1.getOrders(new AsyncCallback<List<Order>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Gond van people");

			}

			public void onSuccess(List<Order> result) {
				int i = 1;
				Iterator<Order> it = result.iterator();
				while (it.hasNext()) {
					Order distr = (Order) it.next();

					g.setText(i, 0, distr.getWho());
					g.setText(i, 1, distr.getUserName());
					g.setText(i, 2, distr.getDate());
					g.setText(i, 3, String.valueOf(distr.getNr()));
					i++;

				}

			}

		});
	}

	/*
	 * public void initButtonPanel(){ newButton = new
	 * Button("\u00DAj rendel\u00E9s"); newButton.addClickListener(new
	 * ClickListener() { public void onClick(Widget sender) {
	 * updateButtonColor(1,0,0); simplePanel.clear(); simplePanel.add(newPanel);
	 * }}); buttonPanel.add(newButton); newButton.setWidth("226px");
	 * 
	 * updateButton = new Button("Rendel\u00E9s m\u00F3dos\u00EDt\u00E1sa");
	 * updateButton.addClickListener(new ClickListener() { public void
	 * onClick(Widget sender) { updateButtonColor(0,1,0); simplePanel.clear();
	 * simplePanel.add(updatePanel); }}); buttonPanel.add(updateButton);
	 * updateButton.setWidth("220px");
	 * 
	 * deleteButton = new Button("Rendel\u00E9s t\u00F6rl\u00E9se");
	 * deleteButton.addClickListener(new ClickListener() { public void
	 * onClick(Widget sender) { updateButtonColor(0,0,1); simplePanel.clear();
	 * simplePanel.add(deletePanel); }}); buttonPanel.add(deleteButton);
	 * deleteButton.setWidth("236px");
	 * 
	 * }
	 */

	public void initNewPanel() {
		FlexTable fl = new FlexTable();
		newPanel.add(fl);
		fl.setSize("684px", "38px");

		Label vnLabel = new Label("Rendel\u0151");
		fl.setWidget(0, 0, vnLabel);
		vnLabel.setWidth("225px");

		lblNewLabel_1 = new Label("Darabsz\u00E1m");
		fl.setWidget(0, 1, lblNewLabel_1);
		Label cimLabel = new Label("D\u00E1tum");
		fl.setWidget(0, 2, cimLabel);

		comboBox = new ListBox();
		comboBox.addItem("");
		fl.setWidget(1, 0, comboBox);
		c2.getDistributors(new AsyncCallback<List<Distributor>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error in Order");
			}

			@Override
			public void onSuccess(List<Distributor> result) {
				Iterator<Distributor> it = result.iterator();
				while (it.hasNext()) {
					Distributor distr = (Distributor) it.next();
					comboBox.addItem(distr.getUserName());
				}
			}
		});

		integerBox = new IntegerBox();
		fl.setWidget(1, 1, integerBox);

		btnNewButton = new Button("New button");
		btnNewButton.setText("Ment\u00E9s");
		btnNewButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent sender) {
				String userName = comboBox.getItemText(comboBox
						.getSelectedIndex());
				int nr = integerBox.getValue();
				if ((userName.length() == 0) || (nr < 1)) {
					Window.alert("Hibas adatok");
				} else {
					comboBox.setSelectedIndex(0);
					integerBox.setText("");

					Order o = new Order(null, userName, "2012-05-12", nr);
					c1.insertOrder(o, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Gond");

						}

						@Override
						public void onSuccess(Void result) {
							Window.alert("Sikeres");
							gridPanel.clear();
							initGrid();

						}

					});
				}

			}

		});

		dateBox = new DateBox();
		fl.setWidget(1, 2, dateBox);
		fl.setWidget(1, 3, btnNewButton);

	}

	/*
	 * public void initUpdatePanel(){ FlexTable fl2 = new FlexTable();
	 * updatePanel.add(fl2); fl2.setSize("677px", "38px");
	 * 
	 * Label lblNewLabel = new
	 * Label("A m\u00F3dos\u00EDtand\u00F3 szem\u00E9ly:"); fl2.setWidget(0, 0,
	 * lblNewLabel);
	 * 
	 * Label vn2Label = new Label("Vezet\u00E9kn\u00E9v"); fl2.setWidget(0, 2,
	 * vn2Label); Label cim2Label = new Label("C\u00EDm"); fl2.setWidget(0, 4,
	 * cim2Label);
	 * 
	 * 
	 * 
	 * final TextBox vn2textBox = new TextBox(); fl2.setWidget(1, 2,
	 * vn2textBox);
	 * 
	 * }
	 * 
	 * public void initDeletePanel(){ FlexTable fl3 = new FlexTable();
	 * deletePanel.add(fl3); fl3.setSize("676px", "38px");
	 * 
	 * Label who2Label = new Label("Kit akar t\u00F6r\u00F6lni?");
	 * fl3.setWidget(0, 2, who2Label); who2Label.setWidth("225px");
	 * 
	 * }
	 */

}
